import globalData from '@/staticData/globalData'
import { getContractSearchUrlApi, batchExportContractApi } from '@/api/contract/contractSearch'

// 多选
const multipleSelection = ref([])
// 导出dialog
const exportDialogRef = ref()
// 允许作废dialog
const allowCancelDialogRef = ref()
// 取消作废dialog
const canCancelDialogRef = ref()
// 查看合同
const showPdfDialogRef = ref()
// 合同生命周期
const openLifeCircleRef = ref()

export default function useSortData() {
  // 合同状态class
  const rowContractStatusColor = computed(() => {
    return status => {
      return globalData.CONTRACT_STATUS_COLOR[status]
    }
  })
  // 导出
  const exportContract = async() => {
    const res = await batchExportContractApi({
      contractIdList: multipleSelection.value
    })
    if (res) {
      exportDialogRef.value.show(res)
    }
  }
  // 判断checkbox是否禁用
  const selectable = row => {
    return (row.contractStatus == 'canceling' || row.contractStatus == 'signed')
  }
  // 全选
  const selectQuestion = val => {
    multipleSelection.value = val.map(item => item.id)
  }
  // 查看合同
  const viewContract = async id => {
    const res = await getContractSearchUrlApi(id)
    console.log('查看合同', res)
    showPdfDialogRef.value.show(res)
  }
  // 1-允许作废
  const allowCancel = id => {
    console.log('允许作废')
    allowCancelDialogRef.value.show(id)
  }
  // 2-撤销作废
  const canCancel = id => {
    console.log('撤销作废')
    canCancelDialogRef.value.show(id)
  }
  // 合同生命周期
  const openLifeCircleFn = () => {
    openLifeCircleRef.value.show()
  }

  return {
    exportDialogRef,
    allowCancelDialogRef,
    canCancelDialogRef,
    showPdfDialogRef,
    openLifeCircleRef,
    rowContractStatusColor,
    multipleSelection,
    exportContract,
    selectable,
    selectQuestion,
    viewContract,
    allowCancel,
    canCancel,
    openLifeCircleFn
  }
}
